package com.cn.wkditui;


import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;



/**
 * Created by wyd on 2016/3/15.
 */
public class AuthActivity extends BaseActivity {
    private TextView tv;
    private LinearLayout l1,l2,l3,l4,l5;
    private EditText etmdname,etlocationd,etname,etphone;
    private TextView etlocation;
    private Button bt;
    private double lat=0;
    private double lng=0;
    private GeoCoder mSearch;
    private TextView tvid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty_auth);
        init();
    }
    private void init() {

        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(listener );
//        tv = (TextView) findViewById(R.id.tv);
//        tv.setText("完善资料");
        l1= (LinearLayout) findViewById(R.id.ll1);
        l2= (LinearLayout) findViewById(R.id.ll2);
        l3= (LinearLayout) findViewById(R.id.ll3);
        l4= (LinearLayout) findViewById(R.id.ll4);
        l5= (LinearLayout) findViewById(R.id.ll5);
        tvid = (TextView) findViewById(R.id.tvid);
        tvid.setText(MyPreference.getInstance(this).getStaffid());
        etmdname = (EditText) findViewById(R.id.etmdname);
        etlocation = (TextView) findViewById(R.id.etlocation);
        etlocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(AuthActivity.this,MapActivity.class),101);
            }
        });


        etlocationd = (EditText) findViewById(R.id.etlocationd);
        etname = (EditText) findViewById(R.id.etname);
        etphone = (EditText) findViewById(R.id.etphone);

        bt = (Button) findViewById(R.id.bt);
        setForcose();

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                l1.setBackground(getResources().getDrawable(R.drawable.item_grey));
                l2.setBackground(getResources().getDrawable(R.drawable.item_grey));
                l3.setBackground(getResources().getDrawable(R.drawable.item_grey));
                l4.setBackground(getResources().getDrawable(R.drawable.item_grey));
                l5.setBackground(getResources().getDrawable(R.drawable.item_grey));
                if(TextUtils.isEmpty(etmdname.getText().toString())){
                    l1.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etmdname.setHintTextColor(getResources().getColor(R.color.red));
                }else  if(TextUtils.isEmpty(etlocation.getText().toString())){
                    l2.setBackground(getResources().getDrawable(R.drawable.item_red));
//                    et.setHintTextColor(getResources().getColor(R.color.red));
                }else if(TextUtils.isEmpty(etlocationd.getText().toString())){
                    l3.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etlocationd.setHintTextColor(getResources().getColor(R.color.red));
                }else if(TextUtils.isEmpty(etname.getText().toString())){
                    l4.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etname.setHintTextColor(getResources().getColor(R.color.red));
                }else if(TextUtils.isEmpty(etphone.getText().toString())||etphone.getText().toString().length()<11){
                    l5.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etphone.setHintTextColor(getResources().getColor(R.color.red));
                }else{

                    String url = Consts.HOST+Consts.SAVEINFO;
//                    new HttpUtils()
                    RequestParams params = new RequestParams();
                    params.addBodyParameter("token", MyPreference.getInstance(AuthActivity.this).getToken());
                    params.addBodyParameter("name",etmdname.getText().toString());
                    params.addBodyParameter("contacts",etname.getText().toString());
                    params.addBodyParameter("tel",etphone.getText().toString());
                    params.addBodyParameter("lat",lat+"");
                    params.addBodyParameter("lng",lng+"");
                    params.addBodyParameter("province",province);
                    params.addBodyParameter("city",city);
                    params.addBodyParameter("address",etlocationd.getText().toString());
                    params.addBodyParameter("staffId",MyPreference.getInstance(AuthActivity.this).getStaffid());
//                    params.addBodyParameter("staffId",etbh.getText().toString());
//									System.out.println("id"+MyPreference.getInstance(AuthActivity.this).getUser_Id());
                    System.out.println("name"+etmdname.getText().toString());
                    System.out.println("contacts"+etname.getText().toString());
                    System.out.println("tel"+etphone.getText().toString());
                    System.out.println("lat"+lat+"");
                    System.out.println("lng"+lng+"");
                    System.out.println("province" +province);
                    System.out.println("city"+city);
//                    System.out.println("address"+name+etstrnum.getText().toString());
//                    System.out.println("staffId"+etbh.getText().toString());

                    HttpUtils httpUtils = new HttpUtils();
                    httpUtils.send(HttpRequest.HttpMethod.POST, url,params, new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            System.out.println("arg1"+arg1);
                            Toast.makeText(getBaseContext(),"网络异常",0  ).show();
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            System.out.println("arg0."+arg0.result);
                            try {
                                JSONObject json = new JSONObject(arg0.result);
                                int code = json.getInt("code");
                                if(code==1){
                                    Toast.makeText(getBaseContext(), "保存成功", 0).show();
                                    MyPreference.getInstance(AuthActivity.this).setToken(json.getJSONObject("data").getString("token"));
                                    Intent intent = new Intent(AuthActivity.this, QAQActivity.class);
                                    intent.putExtra("surveyId",json.getJSONObject("data").getString("vendorId"));
                                    startActivity(intent);
                                    AuthActivity.this.finish();
                                }else{
                                    Toast.makeText(getBaseContext(), json.getString("message"), 0).show();
                                }
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    });
                }


            }
        });
    }

    private String province="";
    private String city="";
    OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
        public void onGetGeoCodeResult(GeoCodeResult result) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                //没有检索到结果
            }

//            Toast.makeText(AuthActivity.this,result.getAddress(),0).show();

            //获取地理编码结果
        }

        @Override
        public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                //没有找到检索结果
            }
            province=   result.getAddressDetail().province;
            city =  result.getAddressDetail().city;
            Log.e("","city"+province+city);
            etlocation.setText(result.getAddress());

//            Toast.makeText(AuthActivity.this, result.getAddressDetail().province,0).show();
            //获取反向地理编码结果
        }
    };
    private void setForcose() {
        etmdname.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    l1.setBackground(getResources().getDrawable(R.drawable.item_blue));
                } else {
                    l1.setBackground(getResources().getDrawable(R.drawable.item_grey));
                }
            }
        });
        etlocation.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    l2.setBackground(getResources().getDrawable(R.drawable.item_blue));
                } else {
                    l2.setBackground(getResources().getDrawable(R.drawable.item_grey));
                }
            }
        });
        etlocationd.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    l3.setBackground(getResources().getDrawable(R.drawable.item_blue));
                } else {
                    l3.setBackground(getResources().getDrawable(R.drawable.item_grey));
                }
            }
        });
        etname.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    l4.setBackground(getResources().getDrawable(R.drawable.item_blue));
                } else {
                    l4.setBackground(getResources().getDrawable(R.drawable.item_grey));
                }
            }
        });
        etphone.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    l5.setBackground(getResources().getDrawable(R.drawable.item_blue));
                } else {
                    l5.setBackground(getResources().getDrawable(R.drawable.item_grey));
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==201){
            l3.setBackground(getResources().getDrawable(R.drawable.item_grey));
            lat=data.getDoubleExtra("lat", 0);
            lng = data.getDoubleExtra("lng", 0);
//            name=data.getStringExtra("name");
//            etlocation.setText(name);
            LatLng latLng = new LatLng(lat,lng);
            mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
        }


    }
//    private LinearLayout ll2;
//    private TextView tv2;
//    private Button btcommit;
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.acitivty_auth);
//        init();
//        btcommit.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivity(new Intent(AuthActivity.this,QAQActivity.class));
//            }
//        });
//    }
//
//    private void init() {
//        tv2 = (TextView) findViewById(R.id.tv2);
//        btcommit= (Button) findViewById(R.id.btcommit);
//        ll2 = (LinearLayout) findViewById(R.id.ll2);
//        ll2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(AuthActivity.this,MapActivity.class);
//                startActivityForResult(intent,102);
//            }
//        });62784 33654
//    }
}
